#include "udf_global.h"

#if (UDF_EN_13 > 0 && !defined(UDP_SUB_PROJECT) && !defined(UDT_SUB_PROJECT) && (defined(UDF13_SUB_PROJECT) || defined(UDF_PRJ)))

#include "udsf.h"

#include "./udsf/udsf_uart.c"  // udsf 用户自定义 子函数
#include "./udsf/udsf_delay.c"
#include "./udsf/udsf_spi.c"
#include "./udsf/udsf_crc.c"
#include "./udsf/udsf_gpio.c"

#include "udf07_process.c"
#include "udf08_process.c"
#include "udf13_process.c"

/*本demo 为兼容动态加载功能，故采用udf13_handle内 实现 udf13、udf08、udf05、udf07回调功能 */
void udf13_udf_handle(void){
	
	/* 修改UDP参数 使UDF08、UDF07回调udf13_handle */
	if( paramRSV->startup_flag ){
		paramRSV->startup_flag = 0;

		sUDF_global.udf_tick=0;
		sUDF_global.udf_tick_rcd = 0;
		sUDF_global.udf13_len = paramFunc->udf13_len;

		sUDF_global.work_mode = 13; // 首次为 udf13调用
		
		#if (DEBUG_DATA_INTERFACE == 1) || ( POINTRST_INTERFACE == 1) || (LOG_INTERFACE == 1) || ( CUSTOM_DATA_INTERFACE == 1 )
		udsf_spi_init();
		#endif
		
		#if (DYNALOAD_FW == 1)
			/*rom默认115200 动态加载设置波特率为udp定义*/
			#if UART_BAUDRATE == 0
			udsf_uart_init(paramFunc->baudrate);
			#endif
		#endif
		
		#if CODE_LOAD_ONCE == 1
		paramFunc->udf13_len =  0xFFFE;
		paramFunc->funcEn &= ~(1<<5); // clear func crc enable
		paramFunc->qspiMode = 1;      // qspi mode
		#endif

		paramFunc->udf07_addr = paramFunc->udf13_addr;
		paramFunc->udf07_len  = paramFunc->udf13_len;
		paramFunc->patchEn |= 1<<(7-1);
		
		#if SIMU_UDF08_ENABLE==1
		paramFunc->udf08_addr = paramFunc->udf07_addr;
		paramFunc->udf08_len  = paramFunc->udf07_len;
		paramFunc->patchEn |= 1<<(8-1);
		#endif
		
		#if SIMU_UDF05_ENABLE==1
		paramFunc->udf05_addr = paramFunc->udf07_addr;
		paramFunc->udf05_len  = paramFunc->udf07_len;
		paramFunc->patchEn |= 1<<(5-1);
		#endif

		/*cal crc*/
		uint16_t crc = udsf_get_crc((uint8_t *)paramFunc,paramFunc->crcLen);
		paramFunc->crcVal = crc;
		LOG_SEND_STRING(LOG_LEVEL_DEBUG,"udf init done\n");

		#if defined(LED_TRIGGER_ENABLE)
		//sUDF_global.gpio_output_lck = 0;
		udsf_pinctrl_ae(1<<LED_TRIGGER_PIN,0); // 数字			
		#if (LED_TRIGGER_IFS==0)
		udsf_pinctrl_od(1<<LED_TRIGGER_PIN,1); // od
		#else
		udsf_pinctrl_od(1<<LED_TRIGGER_PIN,0); // pp
		#endif
		udsf_gpio_output(LED_TRIGGER_PIN,LED_TRIGGER_ACTIVE);  // 默认有效
		udsf_gpio_output_ctrl(1<<LED_TRIGGER_PIN,1);           // 输出
		#endif

		if( !paramDataPrep->switch_flag ){
			udsf_uart_send_string("version:");
			udsf_uart_send_string(FW_VERSION);
		}
		paramDataPrep->switch_flag = 0;
	}
	
	if( paramDataPrep->switch_flag ){
		#if CODE_LOAD_ONCE==1
		paramFunc->udf13_len = 0xFFFE;
		paramFunc->funcEn &= ~(1<<5); // clear func crc enable
		paramFunc->qspiMode = 1;      // qspi mode
		#endif
		
		sUDF_global.work_mode = 7;		
		paramFunc->patchEn &= (~(1<<(3-1)));	
		uint16_t crc = udsf_get_crc((uint8_t *)paramFunc,paramFunc->crcLen);
		paramFunc->crcVal = crc;
	}
	
	/* udf13 callback */
	if( sUDF_global.work_mode == 13 ){

		udsf_udf13_handle();
		
		#if SIMU_UDF08_ENABLE==1
		sUDF_global.work_mode = 8; // next time-> udf08
		#else
		#if (SIMU_UDF05_ENABLE == 1)
		sUDF_global.work_mode = 5; // next time-> udf05
		#else
		sUDF_global.work_mode = 7; // next time-> udf07
		#endif
		#endif
		return;
	}
	
	#if (SIMU_UDF08_ENABLE == 1 )
	/*udf08 callback*/
	if( sUDF_global.work_mode == 8 ){
		udsf_udf08_handle();

		#if (SIMU_UDF05_ENABLE == 1)
		sUDF_global.work_mode = 5; // next time-> udf05
		#else
		sUDF_global.work_mode = 7; // next time-> udf07
		#endif
		return;
	}
	#endif

	#if (SIMU_UDF05_ENABLE == 1 )
	if( sUDF_global.work_mode == 5) {
		sUDF_global.work_mode = 7;
		return;
	}
	#endif
	
	udsf_udf07_handle();
		
	sUDF_global.work_mode = 13; // next-time UDF13
		
	return;
}
#endif

